package com.hyz.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hyz.api.AgencyControllerApi;
import com.hyz.pojo.Agency;
import com.hyz.result.AgencyPage;
import com.hyz.result.AgencyResult;
import com.hyz.service.AgencyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/illya")
public class AgencyController implements AgencyControllerApi {
@Autowired
private AgencyService service;

    @Override
    @PostMapping
    @CacheEvict(value = "agencylist",allEntries = true)
    public AgencyResult save( @RequestBody  Agency agency) {
        if (agency==null){
            AgencyResult.error("服务器繁忙");
        }
        boolean flag = service.save(agency);
        return flag?AgencyResult.success("审核通过，欢迎你~"):AgencyResult.error("手机号已被注册");
    }

    @Override
    @GetMapping("/page")
    @Cacheable(value = "agencypage",key="'agencylist'+#page+#pageSize")
    public AgencyPage<Agency> getByPage(Integer page, Integer pageSize, String name) {
        IPage<Agency> iPage =service.getByPage(page,pageSize,name);
        return AgencyPage.success("查询成功~",iPage);
    }

    @Override
    @GetMapping("/findById")
    @Cacheable(value = "findById",key = "'findById'+#id")
    public AgencyResult findById(Integer id) {
        if (id==null){
            return AgencyResult.error("服务器繁忙~");
        }
        Agency agency = service.getById(id);
        if (agency==null){
            AgencyResult.error("查无此人");
        }
        return AgencyResult.success("查询成功~",agency);
    }
}
